package com.hjrpc.babyyamall.product.service.impl;

import com.hjrpc.babyyamall.product.dao.AttrAttrgroupRelationDao;
import com.hjrpc.babyyamall.product.dao.AttrDao;
import com.hjrpc.babyyamall.product.entity.AttrAttrgroupRelationEntity;
import com.hjrpc.babyyamall.product.entity.AttrEntity;
import com.hjrpc.babyyamall.product.vo.AttrGroupWithAttrsVo;
import com.hjrpc.babyyamall.product.vo.SkuItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjrpc.common.utils.PageUtils;
import com.hjrpc.common.utils.Query;

import com.hjrpc.babyyamall.product.dao.AttrGroupDao;
import com.hjrpc.babyyamall.product.entity.AttrGroupEntity;
import com.hjrpc.babyyamall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(key)) {
            queryWrapper.eq("attr_group_id", key)
                    .or().like("attr_group_name", key);
        }
        if (catelogId != null && catelogId != 0) {
            queryWrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> listWithAttr(Long catelogId) {
        //查询分类下所有分组
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> resultList = attrGroupEntities.stream().map(entity -> {
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(entity, vo);
            // 根据每个分组查询所有的基础属性
            List<Long> attrIds = attrAttrgroupRelationDao
                    .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_group_id", entity.getAttrGroupId()))
                    .stream().map(x -> x.getAttrId()).collect(Collectors.toList());
            //根据属性ID批量查询属性信息
            List<AttrEntity> attrEntities = attrDao.selectBatchIds(attrIds);
            vo.setAttrs(attrEntities);

            return vo;
        }).collect(Collectors.toList());

        return resultList;
    }

    @Override
    public List<AttrEntity> listAttr(Long attrGroupId) {
        List<Long> attrIds = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                .eq("attr_group_id", attrGroupId)).stream().map(x -> x.getAttrId()).collect(Collectors.toList());

        return attrDao.selectBatchIds(attrIds);
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroup> querySpuGroupDetail(Long spuId, Long catalogId) {
        return this.baseMapper.querySpuGroupDetail(spuId,catalogId);
    }
}